#!/usr/bin/python
#* Copyright (c) 2009, Zac Garrett
#* All rights reserved.
#*
#* Redistribution and use in source and binary forms, with or without
#* modification, are permitted provided that the following conditions are met:
#*	 * Redistributions of source code must retain the above copyright
#*	   notice, this list of conditions and the following disclaimer.
#*	 * Redistributions in binary form must reproduce the above copyright
#*	   notice, this list of conditions and the following disclaimer in the
#*	   documentation and/or other materials provided with the distribution.
#*	 * Neither the name of the Zac Garrett nor the
#*	   names of its contributors may be used to endorse or promote products
#*	   derived from this software without specific prior written permission.
#*
#* THIS SOFTWARE IS PROVIDED BY ZAC GARRETT ''AS IS'' AND ANY
#* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
#* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
#* DISCLAIMED. IN NO EVENT SHALL ZAC GARRETT BE LIABLE FOR ANY
#* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
#* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
#* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#
# Required Software:
# Linux, sensors, nvclock
#
# If your network interface is not eth0 you will need
# to go to the bandwidth section of the script and
# edit it to suit your needs.

#__module_name__ = "System Information by Zac Garrett"
#__module_version__ = "1.02"
#__module_description__ = "Python System Information"
#__module_author__ = "Zac Garrett <zg@zacgarrett.com>"

import os, commands, time
from decimal import *

# OS info
tmp = os.uname()
OS = tmp[0]
kernel = tmp[2]
bit = tmp[4]

# CPU Info
def cpuinfo():
	cpuinfo = commands.getoutput('grep "model name" /proc/cpuinfo')
	cpuinfo = cpuinfo.splitlines()
	cpu = cpuinfo[0]
	cpu = cpu.replace('model name : ', '')
	return cpu

def cputemp():
	cputemp = commands.getoutput('sensors | grep temp1:')
	cputemp = cputemp.split(' ')
	cputmp = cputemp[7]

	casetemp = commands.getoutput('sensors | grep temp3:')
	casetemp = casetemp.split(' ')
	casetmp = casetemp[7]
	return cputmp,  casetmp

# GPU Information
def gpuinfo():
	gpuinfo =  commands.getoutput("/usr/bin/nvclock -T")
	gpuinfo = gpuinfo.splitlines()
	gpucard = gpuinfo[1]
	gputemp = commands.getoutput("nvidia-settings -query GPUCoreTemp | grep Attribute")
	gputemp = gputemp.split(" ")
	gtemp = gputemp[3]
	return gpucard, gputemp

# Memory Information
def meminfo():
	meminfo = commands.getoutput("egrep '(MemTotal:|MemFree:|Buffers:|Cached:|SwapFree:|SwapTotal:)' /proc/meminfo")
	meminfo = meminfo.replace(" ","")
	meminfo = meminfo.replace("kB","")
	meminfo = meminfo.splitlines()

	# Alter the Memory info into megabyte format
	memtotal = int(meminfo[0].replace("MemTotal:",""))//1024
	memfree = int(meminfo[1].replace("MemFree:",""))//1024
	membuffer = int(meminfo[2].replace("Buffers:",""))//1024
	memcache = int(meminfo[3].replace("Cached:",""))//1024
	memusage = memtotal-memfree-memcache
	memswapcache = int(meminfo[4].replace("SwapCached:",""))//1024
	memswaptotal = int(meminfo[5].replace("SwapTotal:",""))//1024
	memswapfree = int(meminfo[6].replace("SwapFree:",""))/1024
	memswapusage = memswaptotal-memswapfree-memswapcache

    # Convert the memory info back into strings so it can be printed
	memtotal = str(memtotal)
	memfree = str(memfree)
	membuffer = str(membuffer)
	memcache = str(memcache)
	memusage = str(memusage)
	memswapcache = str(memswapcache)
	memswaptotal = str(memswaptotal)
	memswapfree = str(memswapfree)
	memswapusage = str(memswapusage)
	return memtotal,  memusage,  memswaptotal,  memswapusage


# Disk Info
def disk():
  diskusetotal = commands.getoutput("df | grep -v Filesystem | awk '{ sum+=$3 / 1024 / 1024}; END { print sum }'")
  disktotal = commands.getoutput("df | grep -v Filesystem | awk '{ sum+=$2 / 1024 / 1024}; END { print sum }'")

  diskusetotal = diskusetotal.split(".")
  diskusetotal = diskusetotal[0]
  disktotal = disktotal.split(".")
  disktotal = disktotal[0]
  
  output = "Disk Usage: " + diskusetotal + "/" + disktotal +"GB"
  return output

# Bandwidth Info
def bw():

  timestart = time.time()
  bwdatastart = commands.getoutput("/sbin/ifconfig eth0 | grep 'RX bytes' | awk '/:/ {print $2, $6}'")
  # Sleeping for any longer causes the client to lock up for that amount of time
  time.sleep(0.5)
  bwdataend = commands.getoutput("/sbin/ifconfig eth0 | grep 'RX bytes' | awk '/:/ {print $2, $6}'")
  timeend = time.time()
  totaltime = timeend-timestart
  bwdatastart = bwdatastart.replace('bytes:','')
  bwdataend = bwdataend.replace('bytes:','')
  bwdatastart = bwdatastart.split(' ')
  bwdataend = bwdataend.split(' ')
  rx = int(bwdataend[0])-int(bwdatastart[0])
  tx = int(bwdataend[1])-int(bwdatastart[1])
  rx = rx/totaltime
  tx = tx/totaltime
  rx = Decimal(str(rx/1024))
  tx = Decimal(str(tx/1024))
  rx = rx.quantize(Decimal('.01'), rounding=ROUND_UP)
  tx = tx.quantize(Decimal('.01'), rounding=ROUND_UP)
  rx = str(rx)
  tx = str(tx)
  output = "Bandwidth: RX " + rx + "kb/s - TX " + tx + "kb/s"
  return output
  
def uptime():
  uptime = commands.getoutput("/usr/bin/uptime")
  return uptime

def osinfo ():
  output  = "OS: " + OS + " " + kernel + " " + bit
  return output

'''
def procinfo ():
  processor = cpuinfo()
  proctemp = cputemp()
  command_procinfo = "CPU: " + processor + " Temp: " + proctemp[0] + " Case Temp: " + proctemp[1]
  witem.command("say " + command_procinfo)

def gpu ():
  gfx = gpuinfo()
  command_gpu = "GPU: " + gfx[0] + " temp: " + gfx[1]  + " board: " + gfx[2]
  witem.command("say " + command_gpu)

def memory ():
  mem = meminfo()
  command_memory = "Memory: " + mem[1] + "/" + mem[0] +"MB Swap " + mem[3] + "/" + mem[2] + "MB"
  witem.command("say " + command_memory)

def temp ():
  proctemp = cputemp()
  gputemp = gpuinfo()
  command_temp = "CPU Temp: " + proctemp[0] + " GPU Temp: " + gputemp[1] + " Case: " + proctemp[1]
  witem.command("say " + command_temp)
'''
def sys ():
  processor = cpuinfo()
  mem = meminfo()
  #gfx = gpuinfo()
  command_sys = "OS: " + OS + " " + kernel + " " + bit + " CPU: " + processor  + " " + disk() + " Memory: " + mem[1] + "/" + mem[0] +"MB Swap " + mem[3] + "/" + mem[2] + "MB"
  return command_sys
